{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-05-06T01:27:39.728023Z",
     "start_time": "2025-05-06T01:27:39.724774Z"
    }
   },
   "source": [
    "from PIL.ImImagePlugin import split\n",
    "from modelscope.msdatasets import MsDataset\n",
    "from modelscope import AutoTokenizer"
   ],
   "outputs": [],
   "execution_count": 42
  },
  {
   "cell_type": "code",
   "id": "2abec2bb492408e0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:27:51.883353Z",
     "start_time": "2025-05-06T01:27:40.350502Z"
    }
   },
   "source": [
    "dataset_name = 'AI-ModelScope/fortune-telling'\n",
    "dataset = MsDataset.load(dataset_name, split=\"train\", subset_name=\"default\")"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-05-06 09:27:40,352 - modelscope - WARNING - Use trust_remote_code=True. Will invoke codes from fortune-telling. Please make sure that you can trust the external codes.\n",
      "2025-05-06 09:27:41,114 - modelscope - WARNING - Use trust_remote_code=True. Will invoke codes from AI-ModelScope/fortune-telling. Please make sure that you can trust the external codes.\n",
      "2025-05-06 09:27:41,114 - modelscope - WARNING - Use trust_remote_code=True. Will invoke codes from AI-ModelScope/fortune-telling. Please make sure that you can trust the external codes.\n",
      "2025-05-06 09:27:41,114 - modelscope - WARNING - Use trust_remote_code=True. Will invoke codes from AI-ModelScope/fortune-telling. Please make sure that you can trust the external codes.\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "cell_type": "code",
   "id": "df027f084b6f9a25",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:27:51.889611Z",
     "start_time": "2025-05-06T01:27:51.885612Z"
    }
   },
   "source": "dataset",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['Question', 'Response', 'Complex_CoT'],\n",
       "    num_rows: 207\n",
       "})"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:27:55.809846Z",
     "start_time": "2025-05-06T01:27:55.807681Z"
    }
   },
   "cell_type": "code",
   "source": [
    "train_prompt_style = \"\"\"以下是描述任务的指令，以及提供进一步上下文的输入。\n",
    "请写出一个适当完成请求的回答。\n",
    "在回答之前，请仔细思考问题，并创建一个逻辑连贯的思考过程，以确保回答准确无误。\n",
    "\n",
    "### 指令：\n",
    "你是一位精通八字算命、 紫微斗数、 风水、易经卦象、塔罗牌占卜、星象、面相手相和运势预测等方面的算命大师。\n",
    "请回答以下算命问题。\n",
    "\n",
    "### 问题：\n",
    "{}\n",
    "\n",
    "### 回答：\n",
    "<思考>\n",
    "{}\n",
    "</思考>\n",
    "{}\"\"\""
   ],
   "id": "b47ae2f10bf1701b",
   "outputs": [],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:28:03.807560Z",
     "start_time": "2025-05-06T01:28:02.408763Z"
    }
   },
   "cell_type": "code",
   "source": [
    "model_name = \"unsloth/Qwen3-0.6B\"\n",
    "tokenizer = AutoTokenizer.from_pretrained(model_name)"
   ],
   "id": "7e7568230af35073",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading Model from https://www.modelscope.cn to directory: C:\\Users\\51165\\.cache\\modelscope\\hub\\models\\unsloth\\Qwen3-0.6B\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2025-05-06 09:28:03,307 - modelscope - WARNING - Using branch: master as version is unstable, use with caution\n",
      "2025-05-06 09:28:03,543 - modelscope - INFO - Target directory already exists, skipping creation.\n"
     ]
    }
   ],
   "execution_count": 46
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:28:05.275389Z",
     "start_time": "2025-05-06T01:28:05.271884Z"
    }
   },
   "cell_type": "code",
   "source": [
    "EOS_TOKEN = tokenizer.eos_token  # 必须添加结束标记\n",
    "\n",
    "def formatting_prompts_func(examples):\n",
    "    # 从数据集中提取问题、复杂思考过程和回答\n",
    "    inputs = examples[\"Question\"]\n",
    "    cots = examples[\"Complex_CoT\"]\n",
    "    outputs = examples[\"Response\"]\n",
    "    texts = []  # 用于存储格式化后的文本\n",
    "    # 遍历每个问题、思考过程和回答，进行格式化\n",
    "    for input, cot, output in zip(inputs, cots, outputs):\n",
    "        # 使用字符串模板插入数据，并加上结束标记\n",
    "        text = train_prompt_style.format(input, cot, output) + EOS_TOKEN\n",
    "        texts.append(text)  # 将格式化后的文本添加到列表中\n",
    "    return {\n",
    "        \"text\": texts,  # 返回包含所有格式化文本的字典\n",
    "    }"
   ],
   "id": "126d1f4d73a10d94",
   "outputs": [],
   "execution_count": 47
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:28:06.606305Z",
     "start_time": "2025-05-06T01:28:06.602966Z"
    }
   },
   "cell_type": "code",
   "source": "dataset",
   "id": "caa1f450af0a937d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dataset({\n",
       "    features: ['Question', 'Response', 'Complex_CoT'],\n",
       "    num_rows: 207\n",
       "})"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 48
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-05-06T01:28:10.948535Z",
     "start_time": "2025-05-06T01:28:10.941535Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dataset = dataset.map(formatting_prompts_func, batched = True)\n",
    "print(dataset)\n",
    "dataset[\"text\"][0]"
   ],
   "id": "8abffbfb8e1bca36",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataset({\n",
      "    features: ['Question', 'Response', 'Complex_CoT', 'text'],\n",
      "    num_rows: 207\n",
      "})\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'以下是描述任务的指令，以及提供进一步上下文的输入。\\n请写出一个适当完成请求的回答。\\n在回答之前，请仔细思考问题，并创建一个逻辑连贯的思考过程，以确保回答准确无误。\\n\\n### 指令：\\n你是一位精通八字算命、 紫微斗数、 风水、易经卦象、塔罗牌占卜、星象、面相手相和运势预测等方面的算命大师。\\n请回答以下算命问题。\\n\\n### 问题：\\n新房装修,大门对着电梯好不好?要如何化解?\\n\\n### 回答：\\n<思考>\\n好的，用户问的是新房装修时大门对着电梯好不好，以及如何化解。首先，我需要回忆一下风水学中关于大门和电梯的相关知识。电梯在风水中属于动气比较强的地方，因为电梯频繁开合，会带来不稳定的气流，也就是所谓的“煞气”。大门是住宅的纳气口，如果正对电梯，可能会让这些不稳定的气流直接冲进家里，影响居住者的健康和财运。\\n\\n接下来，我需要确认用户的具体情况。比如，大门和电梯的距离有多远？是否正对还是稍微偏一点？不过用户没有提供这些细节，所以只能给出一般性的建议。化解的方法通常有几种：屏风或玄关、门帘、五帝钱、植物、八卦镜等。需要逐一解释这些方法的原理和使用方式，同时提醒用户要根据实际情况选择，必要时咨询专业风水师。\\n\\n另外，还要注意语气要亲切，避免使用过于专业的术语，让用户容易理解。同时，要强调这些是传统方法，效果因人而异，保持客观中立。最后，可以建议用户如果情况复杂，最好请专业人士实地查看，这样更稳妥。\\n\\n</思考>\\n根据传统风水学的观点，大门正对电梯易形成\"开口煞\"，电梯频繁升降会扰乱家宅气场。建议化解方案：\\n\\n1. 玄关阻隔法\\n在入门处设置L型屏风或文化砖玄关墙，高度以1.8米为宜，既保持采光又形成缓冲带\\n\\n2. 五行通关法\\n门槛石下埋设五帝钱+白玉葫芦，建议选丙申年铸造的真品古币，配合门楣悬挂九宫八卦镜\\n\\n3. 光影化解术\\n安装磨砂玻璃内推门，门框镶嵌黄铜门槛，每日辰时用海盐净化门廊区域\\n\\n4. 现代科技方案\\n入户区安装智能感应灯带，设置循环播放的流水声效，运用声光电技术平衡磁场\\n\\n需注意电梯井方位与家主命卦的关系，建议提供具体户型平面图进行吉凶方位测算。当代建筑中可采用半透明艺术隔断结合空气净化系统，既符合科学原理又兼顾传统智慧。<|im_end|>'"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 49
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "from datasets import load_dataset\n",
    "# 加载指定的数据集，选择中文语言和训练集的前500条记录\n",
    "ds = load_dataset(\"Conard/fortune-telling\", 'default', split = \"train[0:200]\", trust_remote_code=True)\n",
    "# 打印数据集的列名，查看数据集中有哪些字段\n",
    "print(ds.column_names)"
   ],
   "id": "4cc4c79137460d61",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": [
    "ds = dataset.map(formatting_prompts_func, batched = True)\n",
    "ds[\"text\"][0]"
   ],
   "id": "372ac939470ebd42",
   "outputs": [],
   "execution_count": null
  },
  {
   "metadata": {},
   "cell_type": "code",
   "source": "",
   "id": "d42a694ce9e5e9e6",
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "v312",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
